package Review;

import java.util.Arrays;

public class Test33 {

	public static void main(String[] args) {
        int[] array = {3,32,321};
        (new PrintMinNumber()).printMinNumber(array);
	}
	public static void printMinNumber(int[] array){
		if(array==null||array.length==0){
			return;
		}
		for(int i=0;i<array.length-1;i++){
			for(int j=i+1;j<=array.length-1;j++){
				if(compare(array[i],array[j])>0){
					 int temp = array[i];
					 array[i] = array[j];
					 array[j] = temp;
				}
			}
		}
		for(int i=0;i<array.length;i++){
			System.out.print(array[i]);
		}
		
	}
	public static int compare(int m, int n){
		String str1 = m + "" + n;
		String str2 = n + "" + m;
		return str1.compareTo(str2);
	}

}
class PrintMinNumber{
	public void printMinNumber(int [] a){
		if (a == null || a.length <= 0 ) {
			return ;
		}
		quickSort(a, 0,a.length - 1);
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]);
		}
	}
	public void quickSort(int [] a,int low,int high){
		if (low < high) {
			int index = partition(a, low, high);
			if (low < index) {
				quickSort(a, low, index - 1);
			}else {
				quickSort(a, index + 1, high);
			}
		}
	}
	public int partition(int []a ,int low,int high){
		int pivot = a[low];
		while(low < high){
			while(low < high && compareTo(a[high], pivot) >= 0)
				high--;
			a[low] = a[high];
			while(low < high && compareTo(a[low], pivot) < 0)
				low++;
			a[high] = a[low];
		}
		a[low] = pivot;
		return low;
	}
	public int compareTo(int a,int b){
		String aStr = a  + "" + b;
		String bStr = b  + "" + a;
		return aStr.compareTo(bStr);
	}
}